package com.david.reader.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.david.core.utils.MMKVUtil
import com.david.core.utils.ToastUtils
import com.david.reader.utils.BookHelper
import com.david.reader.utils.FileUtil
import com.david.reader.view.ReaderMenuDialog
import com.github.barteksc.pdfviewer.PDFView
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener
import java.io.File

open class PdfReaderActivity: BaseActivity(), OnPageChangeListener {

    lateinit var pdfView: PDFView

    private lateinit var dialog: ReaderMenuDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onPageChanged(page: Int, pageCount: Int) {
        this.page = page
        this.pageCount = pageCount

        if (this::dialog.isInitialized && dialog.isShowing) {
            dialog.setCurrentPage(page, pageCount)
        }
    }

    override fun onPause() {//在退出前修改，不用每次翻页就存储
        super.onPause()

        MMKVUtil.putInt("${id}_page", page)
        MMKVUtil.putInt("${id}_pageCount", pageCount)

        for (item in BookHelper.instance.loadList()) {
            if (id.equals(item.id)) {
                item.pageCount = pageCount
                item.currentPage = page

                BookHelper.instance.saveChange()
                return
            }
        }
    }

    override fun onBackPressed() {
        setResult(0, intent)

        finish()
    }

//    override fun initView() {
//        super.initView()
//        pdfView = PDFView(this,
//            null)
//
//        pdfView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
//        setContentView(pdfView)
//    }

    override fun getReadView(): View {
        pdfView = PDFView(this, null)

        pdfView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        return pdfView
    }

    override fun initData() {
        super.initData()
        page = MMKVUtil.getInt(  "${id}_page", 0)
        val file = File(FileUtil.getDocumentsPath() +"/"+ name)
        if (!file.exists()) {
            ToastUtils.instance.toast("书籍不存在")
        } else {
            setPdfConfig(pdfView.fromFile(file))
            pageCount = MMKVUtil.getInt("${id}_pageCount", pageCount)
        }
//        setPdfConfig(pdfView.fromFile(File("/storage/emulated/0/Documents/book.pdf")))
    }

    private fun initDialog(){
        if (this::dialog.isInitialized) {
            return
        }
        dialog = ReaderMenuDialog(this)
        dialog.onReaderDialogListener = object : ReaderMenuDialog.OnReaderDialogListener{
            override fun onPrePage() {
                if (page > 0) {
                    pdfView.jumpTo(page - 1, true)
                }
            }

            override fun onNextPage() {
                if (page < pageCount) {
                    pdfView.jumpTo(page + 1, true)
                }
            }

            override fun onNavPage(page: Int) {
                if (page < pageCount) {
                    pdfView.jumpTo(page)
                }
            }

            override fun onDragChangePage(progress: Int) {
                pdfView.positionOffset = progress.toFloat()/100f
            }

        }
    }

    fun setPdfConfig(config:PDFView.Configurator){
        config.onPageChange(this@PdfReaderActivity)
            .defaultPage(0)
            .spacing(5)
            .defaultPage(page)
            .fitEachPage(false)
            .onTap {
                initDialog()
                dialog.setCurrentPage(page, pageCount).show()
                return@onTap true
            }
            .load()
    }
}